import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/application/role_personnel_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/common/department_user_entity.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

/// 敏捷应急审批人员选择
class ApprovalPeopleWidget extends StatefulWidget {
  const ApprovalPeopleWidget({required this.nextNodeExecutor, required this.callback, super.key, this.type});

  final String? type;
  final List<RolePersonnelEntity> nextNodeExecutor;
  final Function() callback; //点击回调
  @override
  State<ApprovalPeopleWidget> createState() => _ApprovalPeopleWidgetState();
}

class _ApprovalPeopleWidgetState extends State<ApprovalPeopleWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      child: Container(
        width: 300.w,
        height: 210.w,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(20.w)),
          gradient: const LinearGradient(
              //渐变位置
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              //渐变颜色[始点颜色, 结束颜色]
              colors: [
                Color(0xffE7EAFF),
                Color(0xffE7EAFF),
                Color(0xffffffff),
              ]),
        ),
        child: Stack(
          alignment: Alignment.center,
          children: [
            Container(
                padding: EdgeInsets.symmetric(horizontal: 30.w, vertical: 12.w),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    Text(
                      '下一节点审批人',
                      style: TextStyle(color: Colors.black, fontSize: 18.sp, fontWeight: FontWeight.bold),
                    ),
                    Expanded(
                      child: Center(
                          child: Container(
                              height: 32.w,
                              padding: EdgeInsets.symmetric(horizontal: 12.w),
                              margin: EdgeInsets.only(bottom: 10.w),
                              alignment: Alignment.center,
                              decoration: BoxDecoration(
                                  color: Colors.white,
                                  border: Border.all(color: const Color(0xff262F38).withOpacity(0.5), width: 1.w),
                                  borderRadius: BorderRadius.all(Radius.circular(8.w))),
                              child: Row(
                                children: [
                                  Expanded(
                                      child: Text(
                                    widget.nextNodeExecutor.map((e) => e.getShowName()).join(','),
                                    maxLines: 1,
                                    overflow: TextOverflow.ellipsis,
                                    style: TextStyle(
                                      fontSize: 14.sp,
                                      fontWeight: FontWeight.w400,
                                      color: widget.nextNodeExecutor.isEmpty ? const Color(0xffA0A7B5) : const Color(0xff262F38),
                                    ),
                                  )),
                                  Icon(Icons.arrow_forward_ios, size: 14.sp, color: const Color(0xff262F38).withOpacity(0.5))
                                ],
                              )).clickOnce(selectApproval)),
                    ),
                    Container(
                        height: 40.w,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          color: const Color(0xff5C6DFF),
                          borderRadius: BorderRadius.all(Radius.circular(20.w)),
                        ),
                        child: Text(
                          '确认',
                          style: TextStyle(
                            fontSize: 16.sp,
                            color: const Color(0xffffffff),
                          ),
                        )).clickOnce(() {
                      if (widget.nextNodeExecutor.isEmpty) {
                        AppToast.show('请选择人员');
                        return;
                      }
                      widget.callback();
                    })
                  ],
                )),
            Positioned(
                top: 2.w,
                right: 8.w,
                child: IconButton(
                  icon: Container(
                    width: 30.w,
                    height: 30.w,
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.all(Radius.circular(15.w)),
                    ),
                    child: Icon(
                      Icons.close,
                      color: Colors.grey,
                      size: 21.w,
                    ),
                  ),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                ))
          ],
        ),
      ),
    );
  }

  /// 选择执行人
  void selectApproval() {
    Get.toNamed(AppRoutes.personnelDepartmentTreePage, arguments: {
      'title': '审批人',
      'type': widget.type ?? '5',
      'isRadio': false,
    })?.then((value) {
      if (value != null) {
        final List<DepartmentUserEntity> userList = value as List<DepartmentUserEntity>;
        widget.nextNodeExecutor.clear();
        for (var item in userList) {
          widget.nextNodeExecutor.add(RolePersonnelEntity()
            ..userName = item.nickname
            ..nickName = item.nickname
            ..userId = item.id);
        }
        setState(() {});
      }
    });
  }
}
